/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dl7.player.media;

import com.dl7.player.utils.LogUtil;
import ohos.agp.components.Component;
import ohos.agp.components.DragInfo;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * 处理触摸监听 响应各种手势
 */

public class HandleTouchEvent {

    public static final int TOUCH_STATUS_DOWN = 0x11;
    public static final int TOUCH_STATUS_MOVE = 0x22;
    public static final int TOUCH_STATUS_END = 0x33;


    private GestureEvent gestureEvent;

    HandleTouchEvent(GestureEvent touchEvent) {
        this.gestureEvent = touchEvent;
    }

    private int moveLen = 30; //最小移动距离，低于此移动距离，不被认定为拖拽事件
    private final float speedrate = 0.70f; //调节进度灵敏度 滑动0.xx屏幕可以到底

    private float downX, downY;
    private int w, h;
    private int leftX, rightX;
    private int type = -1;
    float level = 0;

    long tempTime;

    public boolean handleEvent(Component view, DragInfo dragInfo, int touchStatus) {

        switch (touchStatus) {
            case TOUCH_STATUS_DOWN:
                init();
                downX = dragInfo.downPoint.getPointX();
                downY = dragInfo.downPoint.getPointY();
                w = view.getWidth();
                h = view.getHeight();
                leftX = (int) (w * 0.25);
                rightX = (int) (w * 0.75);
                break;
            case TOUCH_STATUS_MOVE:
                float moveX = dragInfo.updatePoint.getPointX() - downX;
                float moveY = dragInfo.updatePoint.getPointY() - downY;

                //判断触发
                if (type < 0) {
                    float absMoveX = Math.abs(moveX);
                    float absMoveY = Math.abs(moveY);
                    if (absMoveX > moveLen) {
                        type = GestureEvent.TOUCH_FULL_X;
                        gestureEvent.onGestureBegin(type);
                    }
                    if (absMoveY > moveLen && downX <= leftX) {
                        type = GestureEvent.TOUCH_LEFT_Y;
                        gestureEvent.onGestureBegin(type);
                    }
                    if (absMoveY > moveLen && downX > rightX) {
                        type = GestureEvent.TOUCH_RIGHT_Y;
                        gestureEvent.onGestureBegin(type);
                    }
                }

                switch (type) {
                    case GestureEvent.TOUCH_FULL_X:
                        level = 1.0f * moveX / (w * speedrate);
                        break;
                    case GestureEvent.TOUCH_LEFT_Y:
                    case GestureEvent.TOUCH_RIGHT_Y:
                        level = 1.0f * -moveY / (h * speedrate);
                        break;
                }

                gestureEvent.onGestureChange(type, level);
                if (type > 0) {
                    if (level < -1)
                        level = -1;
                    if (level > 1)
                        level = 1;
                    gestureEvent.onGestureChange(type, level);
                }
                break;
            case TOUCH_STATUS_END:
                long l = System.currentTimeMillis();
                long delay = l - tempTime;
                tempTime = l;
                if (delay < 300)
                    type = GestureEvent.TOUCH_DOUBLE_C;

                if (type > 0) {
                    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
                    eventHandler.postTask(() -> gestureEvent.onGestureEnd(type, level));
                }
                break;
        }
        return type > 0 & type != GestureEvent.TOUCH_DOUBLE_C; //双击不拦截事件
    }

    private void init() {
        downX = downY = 0;
        leftX = rightX = 0;
        h = w = 0;
        type = -1;
        level = 0;
    }

    public interface GestureEvent {

        int TOUCH_FULL_X = 1; //全屏左右滑动
        int TOUCH_LEFT_Y = 2; //左边上下滑动
        int TOUCH_RIGHT_Y = 3; //右边上下滑动
        int TOUCH_DOUBLE_C = 4; //双击
        //...

        void onGestureBegin(int type); //触发手势事件

        void onGestureChange(int type, float level); //改变level: -1 ~ 1

        void onGestureEnd(int type, float level); //手势事件结束
    }
}
